#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>
#include <conio.h>
#include <vector>
#include <cstring>
#include <malloc.h>
using namespace std;

/// comment
/// //|| !(strcmp(argv[1], "/on") && strcmp(argv[1], "/off"))

void error();
void tokenByNewLine(char* buffer, vector<string> &vcContent);

int main(int argc, char **argv){
	if(argc == 1 || argc > 2 )
	{
		error();
		return 0;
	}
	
	system("TaskKill /F /IM chrome.exe");
	cout << "Killing processes's done\n";
	
	// 
	bool signature = false;
	vector<string> vcString;
	vector<string>::iterator itBrowser;
	fstream filePreference;
	char buffer[1024];
	char *findBrowser;
	
	// set default value
	memset(buffer, 0, sizeof(buffer));
	vcString.resize(0);
	
	//char filePath[] = "C:\\Users\\truong\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Preferences";
	char filePath[] = "C:\\Users\\truong\\Desktop\\Test.txt";
	//char filePath[] = "C:\\Users\\HIRO\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Preferences";
	
	filePreference.open(filePath, ios::in);
	if(!filePreference.is_open())
	{
		cout << "Can\'t open file." << endl;
		return 0;
	}	
	
	while(!filePreference.eof())
	{
		memset(buffer + 1019, 0, 5);
		// 5 last char of buffer's always NULL, so can insert 4 character to this buffer.
		filePreference.read(buffer, sizeof(buffer) - 5);
		cout << "---------------" << endl;
		cout << buffer << endl;
		/*
		if(!signature)
		{
			if(strstr(buffer, "\xEF\xBB\xBF")){
				vcString.push_back(string("\xEF\xBB\xBF"));
				itBrowser = vcString.begin();
				signature = true;
			}
		}
		*/
		//if(findBrowser == NULL)
		if(1)
		{
			//findBrowser = strstr(buffer, "\"browser\"");
			char *find = strstr(buffer, "show_home_button");
			if(find)
			{
				// find first ',' character
				char *findComma = strstr(find, ",");
				char *strAfterComma = (char*)malloc(strlen(findComma)*sizeof(char)); 
				strcpy(strAfterComma, findComma);
				// is turn on
				if(!strcmp(argv[1], "/on")){
					strcpy(find, "show_home_button\": true,");
					strcpy(find +  sizeof("show_home_button\": true") - 1, strAfterComma);
				}
				// is turn off
				if(!strcmp(argv[1], "/off"))
				{
					strcpy(find, "show_home_button\": false,");
					strcpy(find +  sizeof("show_home_button\": false") - 1, strAfterComma);
				}
				free(strAfterComma);			
				vcString.push_back(string(buffer));
				itBrowser = vcString.end() - 1 ;
				cout << "FOUND" << endl;
				continue;
			}
		}
		vcString.push_back(string(buffer));
		
	}
	/*if(itBrowser!= vcString.begin())
	{
		cout << "FOUND BROWSER" << endl;
	}
	*/
	filePreference.close();
	
	filePreference.open(filePath, ios::out);
	for(int i = 0; i < vcString.size(); i++)
	{
		//cout << vcString[i];
		filePreference << vcString[i];
	}
	
	filePreference.close();
	system("start chrome");
	return 0;
}

void error(){
	cout << "Failed:::: FORMAT: ShowHomwButton.exe /option." << endl;
	cout << "\t/on : change to show home button." << endl;
	cout << "\t/off : change to hide home button." << endl;
}

// To token a char* by new line character:
// Example: Buffer = "Nguyen Truong\nBKHN\n23tuoi."
// -> "Nguyen Quoc Truong" + "\nBKHN" + "\23tuoi."
void tokenByNewLine(char* buffer, vector<string> &vcContent)
{
	string strTemp;
	strTemp.reserve(0);
	int lenght = strlen(buffer);
	for(int i = 0; i < lenght; i++){
		if(buffer[i] == '\n'){
			if(i != 0)
			{
				vcContent.push_back(strTemp);
				strTemp.resize(0);
			}
			strTemp+=buffer[i];
		}
		else
		{
			strTemp+= buffer[i];
		}
		if(i == lenght -1)
			vcContent.push_back(strTemp);
	}
}
